import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../base/log.dart';
import '../../base/theme.dart';
import '../dashed_horizontal_line.dart';
import '../horizontal_line.dart';
import '../jsw_base_view.dart';

///自定义空柱状统计图
class EmptyDrawBarChart extends StatefulWidget {

  const EmptyDrawBarChart({
    super.key,
  });

  @override
  State<StatefulWidget> createState() => _EmptyDrawBarChartState();
}

class _EmptyDrawBarChartState extends State<EmptyDrawBarChart> {

  static const _tag = '[draw_empty_barchart_widget.dart][_EmptyDrawBarChartState]';

  ///近28天的数据
  List<String> _dayDataList = [];


  @override
  void initState() {
    super.initState();
    _dayDataList = _getLastDays(28);
  }

  List<String> _getLastDays(int num) {
    List<String> dates = [];
    final DateTime now = DateTime.now();
    final DateFormat formatter = DateFormat('yyyy.MM.dd');
    for (int i = 27; i >= 0; i--) {
      DateTime date = now.subtract(Duration(days: i));
      dates.add(formatter.format(date));
    }
    return dates;
  }

  /// 将 yyyy.MM.dd 格式的字符串解析为 DateTime 对象
  DateTime _parseYmdDate(String dateString) {
    try {
      final DateFormat formatter = DateFormat('yyyy.MM.dd');
      return formatter.parse(dateString);
    } catch (e) {
      return DateTime.now();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //统计图
        Container(
            width: double.infinity,
            margin:
                const EdgeInsets.only(top: 30, left: 20, right: 20, bottom: 17),
            child: Stack(
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: 12.5,
                      child: Text(
                        "0.0",
                        textAlign: TextAlign.left,
                        style: ThemeUtil.p2.copyWith(
                            fontSize: 9,
                            color: const Color(ThemeUtil.primaryWhiteValue50)),
                      ),
                    ),
                    const DashedHorizontalLine(
                      width: double.infinity,
                      height: 0.5,
                      color: Colors.white24,
                      dashLength: 2,
                      dashGap: 2,
                    ),
                    const SizedBox(
                      height: 37.5,
                    ),
                    SizedBox(
                      height: 12.5,
                      child: Text(
                        "0.0",
                        textAlign: TextAlign.left,
                        style: ThemeUtil.p2.copyWith(
                            fontSize: 9,
                            color: const Color(ThemeUtil.primaryWhiteValue50)),
                      ),
                    ),
                    const DashedHorizontalLine(
                      width: double.infinity,
                      height: 0.5,
                      color: Colors.white24,
                      dashLength: 2,
                      dashGap: 2,
                    ),
                    const SizedBox(
                      height: 37.5,
                    ),
                    SizedBox(
                      height: 12.5,
                      child: Text(
                        "0.0",
                        textAlign: TextAlign.left,
                        style: ThemeUtil.p2.copyWith(
                            fontSize: 9,
                            color: const Color(ThemeUtil.primaryWhiteValue50)),
                      ),
                    ),
                    HorizontalLine(dashedHeight: 1.5, color: Colors.white24),
                    const SizedBox(
                      height: 37.5,
                    ),
                    SizedBox(
                      height: 12.5,
                      child: Text(
                        "0.0",
                        textAlign: TextAlign.left,
                        style: ThemeUtil.p2.copyWith(
                            fontSize: 9,
                            color: const Color(ThemeUtil.primaryWhiteValue50)),
                      ),
                    ),
                    const DashedHorizontalLine(
                      width: double.infinity,
                      height: 0.5,
                      color: Colors.white24,
                      dashLength: 2,
                      dashGap: 2,
                    ),
                    const SizedBox(
                      height: 37.5,
                    ),
                    SizedBox(
                      height: 12.5,
                      child: Text(
                        "0.0",
                        textAlign: TextAlign.left,
                        style: ThemeUtil.p2.copyWith(
                            fontSize: 9,
                            color: const Color(ThemeUtil.primaryWhiteValue50)),
                      ),
                    ),
                    const DashedHorizontalLine(
                      width: double.infinity,
                      height: 0.5,
                      color: Colors.white24,
                      dashLength: 2,
                      dashGap: 2,
                    ),
                  ],
                ),
              ],
            )),

        //日期
        Container(
            width: double.infinity,
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 17, top: 20),
            child: Stack(
              children: [
                JSWLine(
                  clickable: false,
                  margin: const EdgeInsets.only(left: 20.0, top: 1),
                  children: [
                    for (int i = 0; i < _dayDataList.length; i++)
                    _buildDrawDate(date: i % 2 == 0 ? _parseYmdDate(_dayDataList[i]).day.toString() : "")
                  ],
                ),
              ],
            )),
      ],
    );
  }

  ///构建日期
  Widget _buildDrawDate({required String date}) {
    return BaseViewItem(
        weight: 1,
        child: Text(
          date,
          textAlign: TextAlign.center,
          style: ThemeUtil.p2.copyWith(
              fontSize: 8, color: const Color(ThemeUtil.primaryWhiteValue50)),
        ));
  }

}
